﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using ThousandFunClient.model;
using System.Reflection;
using System.Windows.Forms;
using System.Collections;
using System.Management;
using System.Net;

namespace ThousandFunClient.Util
{
    class ConfigUtil
    {

        //获取cpuid
        public static bool valRegStr(string regStr,string version)
        {
            try
            {
                string decryptstr = CryptographyUtil.Decrypt(regStr);
                string timereg = decryptstr.Substring(0, 8);
                string mac = decryptstr.Substring(8, decryptstr.Length-8);
                DateTime dt = System.DateTime.Now;
                string timespan = string.Format("{0:yyyyMMdd}", dt);
                if (int.Parse(timespan) > int.Parse(timereg))
                {
                    return false;
                }

                if (!version.Equals("0.0.0.0"))
                {
                    if (!mac.Equals(GetMacAddress()))
                    {
                        return false;
                    }
                }
            }
            catch (Exception exc)
            {
                Constants.syslogger.Error(exc.ToString());
                return false;
            }
            return true;

        }
        //获取cpuid
        public static  string GetCpuID()
        {
            string CpuInfo = "";
            ManagementClass cimobject = new
            ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = cimobject.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                CpuInfo = mo.Properties["ProcessorId"].Value.ToString();
            }
            return CpuInfo;
        }
        //获取硬盘id
        public static string GetHardID()
        {
            string HDInfo = "";
            ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();
            foreach (ManagementObject mo in moc1)
            {
                HDInfo = (string)mo.Properties["Model"].Value;
            }
            return HDInfo;
        }

        
            //获得配置的IP号
        public static String GetToolsNetIP()
        {
            try
            {
                string ToolsNetIP = System.Configuration.ConfigurationSettings.AppSettings["ToolsNetIP"];
                return ToolsNetIP;

            }
            catch { return "192.168.1.103"; }//默认是6575
        }

        //获得配置的端口号
        public static int GetToolsNetPort()
        {
            try
            {
                string TPort = System.Configuration.ConfigurationSettings.AppSettings["ToolsNetPort"];
                return Convert.ToInt32(TPort);

            }
            catch { return 6575; }//默认是6575
        }
        //获得本机地址
        public static string GetNowIPAddress()
        {
            System.Net.IPAddress addr;
            // 获得本机局域网IP地址
            addr = new System.Net.IPAddress(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].Address);
            return addr.ToString();
        }
        //获取网卡物理地址
        public static string GetMacAddress()
        {
            string MacAddress = "";
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc2 = mc.GetInstances();
            foreach (ManagementObject mo in moc2)
            {
                if ((bool)mo["IPEnabled"] == true)
                    MacAddress = mo["MacAddress"].ToString();
                mo.Dispose();
            }
            return MacAddress;
        }

        //设置记录到xml文件
        public static void xmlUpdate()
        {
            if (File.Exists(TFConfig.ConfigXMLAddress)) //文件存在就删除
            {
                FileInfo file = new FileInfo(TFConfig.ConfigXMLAddress);
                file.Delete();
            }

            XElement config = new XElement("ThousandFunConfig");

                XElement el = new XElement("PortA", 
                       new XElement("PortName", TFConfig.GetInstance().PortConfigA.PortName),
                       new XElement("Handshake", TFConfig.GetInstance().PortConfigA.Handshake),
                       new XElement("StopBits", TFConfig.GetInstance().PortConfigA.StopBits),
                       new XElement("DataBits", TFConfig.GetInstance().PortConfigA.DataBits),
                       new XElement("BaudRate", TFConfig.GetInstance().PortConfigA.BaudRate),
                       new XElement("Parity", TFConfig.GetInstance().PortConfigA.Parity),
                       new XElement("ReadTimeout", TFConfig.GetInstance().PortConfigA.ReadTimeout)
                       );
                config.Add(el);

                XElement elb = new XElement("PortB",
                       new XElement("PortName", TFConfig.GetInstance().PortConfigB.PortName),
                       new XElement("Handshake", TFConfig.GetInstance().PortConfigB.Handshake),
                       new XElement("StopBits", TFConfig.GetInstance().PortConfigB.StopBits),
                       new XElement("DataBits", TFConfig.GetInstance().PortConfigB.DataBits),
                       new XElement("BaudRate", TFConfig.GetInstance().PortConfigB.BaudRate),
                       new XElement("Parity", TFConfig.GetInstance().PortConfigB.Parity),
                       new XElement("ReadTimeout", TFConfig.GetInstance().PortConfigB.ReadTimeout)
                       );

                config.Add(elb);

                XElement elbc = new XElement("TorqueConfig",
                           new XElement("CMD", TFConfig.GetInstance().TorqueConfig.SendCMD),
                           new XElement("Interval", TFConfig.GetInstance().TorqueConfig.Interval),
                           new XElement("LoggerName", TFConfig.GetInstance().TorqueConfig.LoggerName),
                           new XElement("ReadTimeout", TFConfig.GetInstance().TorqueConfig.ReadTimeout)
                           );
                config.Add(elbc);

                XElement elbcg = new XElement("AngleConfig",
                          new XElement("CMD", TFConfig.GetInstance().AngleConfig.SendCMD),
                           new XElement("Interval", TFConfig.GetInstance().AngleConfig.Interval),
                           new XElement("LoggerName", TFConfig.GetInstance().AngleConfig.LoggerName),
                           new XElement("ReadTimeout", TFConfig.GetInstance().AngleConfig.ReadTimeout)
               );


                config.Add(elbcg);

                Hashtable ht = TFConfig.GetInstance().HtBatchCMD;

                XElement eCMD = new XElement("BatchCMD");

                foreach (DictionaryEntry de in ht) //ht为一个Hashtable实例
                {
                    SendCMDConfig sc = (SendCMDConfig)de.Value;
                    XElement eSendCMD = new XElement("SendCMD",
                     new XElement("CMD", sc.SendCMD),
                       new XElement("Interval", sc.Interval),
                       new XElement("LoggerName", sc.LoggerName)
                       );
                    eCMD.Add(eSendCMD);
                }
                

                config.Add(eCMD);
                config.Save(TFConfig.ConfigXMLAddress);
            }


        //建立xml设置文件
        public static void initConfig()
        {
            if (File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + "thousanfunsendfinal.csv"))
            {
                FileInfo filec = new FileInfo(System.AppDomain.CurrentDomain.BaseDirectory + "thousanfunsendfinal.csv");
                filec.Delete();
            }

            if (!File.Exists(TFConfig.ConfigXMLAddress)) //文件不存在就新建
            {
                XElement config = new XElement("ThousandFunConfig");

                XElement elv = new XElement("Version",
                       new XElement("Register", "")
             
                       );
                config.Add(elv);

                XElement el = new XElement("PortA", 
                       new XElement("PortName", "com7"),
                       new XElement("Handshake", ""),
                       new XElement("StopBits", ""),
                       new XElement("DataBits", ""),
                       new XElement("BaudRate", "57600"),
                       new XElement("Parity", ""),
                       new XElement("ReadTimeout", "2000")
                       );
                config.Add(el);

                XElement elb = new XElement("PortB",
                       new XElement("PortName", "com7"),
                       new XElement("Handshake", ""),
                       new XElement("StopBits", ""),
                       new XElement("DataBits", ""),
                       new XElement("BaudRate", "57600"),
                       new XElement("Parity", ""),
                       new XElement("ReadTimeout", "2000")
                       );

                config.Add(elb);

                XElement elc = new XElement("PortC",
                       new XElement("PortName", "com5"),
                       new XElement("Handshake", ""),
                       new XElement("StopBits", ""),
                       new XElement("DataBits", ""),
                       new XElement("BaudRate", "9600"),
                       new XElement("Parity", ""),
                       new XElement("ReadTimeout", "2000")
                       );
                config.Add(elc);
                
                XElement elbcy = new XElement("CycleNumberConfig",
                       new XElement("CMD", "00 06 02 01 0B 00 40 43 68"),
                       new XElement("Interval", "1000"),
                       new XElement("LoggerName", "ThousandFun.angle"),
                       new XElement("ReadTimeout", "2000")
                       );
                config.Add(elbcy);


                XElement elbc = new XElement("TorqueConfig",
                       new XElement("CMD", "00 06 02 01 0B 00 40 10 9B"),
                       new XElement("Interval", "1000"),
                       new XElement("LoggerName", "ThousandFun.angle"),
                       new XElement("ReadTimeout", "2000")
                       );
                config.Add(elbc);

                XElement elbcg = new XElement("AngleConfig",
               new XElement("CMD", "00 06 02 01 0B 00 40 20 8B"),
               new XElement("Interval", "1000"),
               new XElement("LoggerName", "ThousandFun.angle"),
               new XElement("ReadTimeout", "2000")
               );


                config.Add(elbcg);


                XElement eSendCMD = new XElement("SendCMD",
                     new XElement("CMD", "00 06 02 01 0B 00 40 43 68"),
                       new XElement("Interval", "1000"),
                       new XElement("LoggerName", "ThousandFun.angle")
                       );

                XElement eSendCMD1 = new XElement("SendCMD",
                     new XElement("CMD", "00 06 02 01 0B 00 40 40 6B"),
                       new XElement("Interval", "2000"),
                       new XElement("LoggerName", "ThousandFun.angle")
                       );

                XElement eSendCMD2 = new XElement("SendCMD",
                     new XElement("CMD", "00 06 02 01 0B 00 40 41 6A"),
                       new XElement("Interval", "3000"),
                       new XElement("LoggerName", "ThousandFun.angle")
                       );
                
                XElement eSendCMD3 = new XElement("SendCMD",
                 new XElement("CMD", "00 06 02 01 0B 00 40 42 69"),
               new XElement("Interval", "4000"),
               new XElement("LoggerName", "ThousandFun.angle")
             );

                XElement eCMD = new XElement("BatchCMD",
                        eSendCMD,
                       eSendCMD1,
                       eSendCMD2,
                       eSendCMD3
                       );


                
                config.Add(eCMD);


                config.Save(TFConfig.ConfigXMLAddress);
            }


            
        }

       

    }
}
